package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;
    @RequestMapping("list")
    public BaseRespVo list(CouponBO couponBO){
        ListData<Coupon> couponListData = couponService.queryList(couponBO);
        return BaseRespVo.ok(couponListData);
    }


    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Coupon coupon){
        if (coupon.getEndTime()!=null&&new Date().getTime()>coupon.getEndTime().getTime()){
            return BaseRespVo.fail("添加时间异常");
        }
        Coupon insertCoupon = couponService.insertCoupon(coupon);
        return BaseRespVo.ok(insertCoupon);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Coupon coupon){
        couponService.updateCoupon(coupon);
        Coupon queryCoupon = couponService.queryCoupon(coupon.getId());
        return BaseRespVo.ok(queryCoupon);
    }

    @RequestMapping("read")
    public BaseRespVo read(Integer id){
       Coupon coupon = couponService.queryCoupon(id);
       return BaseRespVo.ok(coupon);
    }

    @RequestMapping("listuser")
    public BaseRespVo listUser(CouponUserBO couponUserBO){
        ListData<CouponUser> couponUserListData = couponService.selectList(couponUserBO);
        return BaseRespVo.ok(couponUserListData);
    }


    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Coupon coupon){
        couponService.deleteById(coupon.getId());
        return BaseRespVo.ok();
    }


}
